﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BenefitHelper.Sys
{
    public class Product
    {
        public int Id { get; set; }

        public string ProductId { get; set; }
        /// <summary>
        /// 品种名称
        /// </summary>
        public string ProductName { get; set; }
        /// <summary>
        /// 交易所
        /// </summary>
        public int ExchangeID { get; set; }

        public virtual Exchange Exchange { get; set; }

        /// <summary>
        /// 获取所有品种
        /// </summary>
        /// <returns></returns>
        internal List<Product> GetList(DB.DBManager db)
        {
            var query = from t in db.Product select t;
            List<Product> products = query.ToList();
            return products;
        }
        /// <summary>
        /// 保存品种
        /// </summary>
        /// <returns></returns>
        public int InsertIntoBenefitFromServer(string productId, string exchangeId, DB.DBManager db)
        {
            Sys.Product p = new Product();
            p.ProductId = productId;
            var query = from t in db.Exchange where t.ExchangeId.Equals(exchangeId) select t;
            if (query.Count() > 0)
            {
                p.ExchangeID = query.First().Id;
            }
            db.Product.Add(p);
            db.SaveChanges();
            int id = p.Id;
            return id;
        }

        /// <summary>
        /// 获取分析系统里面的品种的编号
        /// </summary>
        /// <param name="Instrument"></param>
        /// <returns></returns>
        public int GetProductId(string productId, DB.DBManager db)
        {
            int id = 0;
            var query = db.Product.Where(a => a.ProductId.Equals(productId));
            if (query.Count() > 0)
            {
                id = query.First().Id;
            }
            return id;
        }
    }
}